package com.dingreading.cloud.admin.util.jwt;

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.dingreading.cloud.admin.dto.HeadStaffDto;
import com.dingreading.cloud.admin.entity.HeadStaff;
import com.dingreading.cloud.admin.entity.Staff;
import com.dingreading.cloud.common.util.SHAUtil;
import com.google.common.collect.Lists;

import java.util.List;

/**
 * token生成工具
 */
public class JwtHelper {

    /**
     * 总部员工token的生成方法
     *
     * @return string
     */
    public static String getHeadToken(String staffUid,String loginTime) {
        String token = JWT.create().withAudience(String.valueOf(staffUid))
                .sign(Algorithm.HMAC256(loginTime));
        return token;
    }

    /**
     * 门店管理端token的生成方法
     *
     * @param staff
     * @return
     */
    public static String getStaffToken(Staff staff) {
        String token = "";
        token = JWT.create().withAudience(String.valueOf(staff.getId())).sign(Algorithm.HMAC512(staff.getLastLoginTime()));
        return token;
    }

    public static String getStaffToken2(Staff staff) {
        String dealString = dealString(staff);
        String sha512 = new SHAUtil().SHA512(dealString);
        String token = staff.getUid().concat(sha512);
        return token;
    }

    public static String dealString(Staff staff) {
        return staff.getUid().concat("_").concat("HSZY0551BHHF").concat("_").concat(staff.getLastLoginTime());
    }

    public static List<String> subToken(String token) {

        String uid = token.substring(0, 32);
        String tokenStr = token.substring(32);
//        System.out.println("split = " + substring);
//        System.out.println("split = " + substring2);
        return Lists.newArrayList(uid, tokenStr);
    }

}
